package com.topscomm.mpm.controller;

import com.topscomm.cbo.controller.CboAttachmentController;
import com.topscomm.mpm.pojo.MpmProjectTaskFeedbackEntity;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Description TODO
 * @Classname MpmAttachmentController
 * @Date 2022/3/30
 * @Created by jiangtao
 */
@RestController
@RequestMapping("mpm/attachment")
public class MpmAttachmentController extends CboAttachmentController {
	/**
	 * @description: 列表查询
	 * @param paraMap
	 * @return
	 * @author: donghang
	 * @date: 2019年8月15日上午8:43:14
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_name")) {
			String value = paraMap.get("qry_name").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and name like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_enabled")) {
			String value = paraMap.get("qry_enabled").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and enabled = 1 ", value);
		}
		if (paraMap.containsKey("qry_code")) {
			String value = paraMap.get("qry_code").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and code like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_sourceType")) {
			String value = paraMap.get("qry_sourceType").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and sourcetype = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_attachmentTypeId")) {
			String value = paraMap.get("qry_attachmentTypeId").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and attachmenttypeid = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_sourceId")) {
			String value = paraMap.get("qry_sourceId").toString();
			if (!StringUtil.isEmpty(value)){
				sql.appendSingle(" and sourceid in (select id from {0} where taskid = {1} and state >0 )", MpmProjectTaskFeedbackEntity.tableName,value);
			}
		}
		return sql.toString();
	}

	@GetMapping(value = "/calculateCount")
	public ResponseResult<Object> calculateCount(@RequestParam Map<String, Object> paraMap) {
		paraMap = BaseUtil.decodeSecureMap(paraMap);
		String sourceType = paraMap.get("sourceType").toString();
		String sourceId = paraMap.get("sourceId").toString();
		int count = 0;
		if (!StringUtil.isEmpty(sourceType) && !StringUtil.isEmpty(sourceId)) {
			StringBufferProxy sql = new StringBufferProxy();
			sql.appendSingle(" sourceType = '{0}' ", sourceType);
			sql.appendSingle(" and sourceid in (select id from {0} where taskid = {1} and state >0 )", MpmProjectTaskFeedbackEntity.tableName,sourceId);
			count = service.getCount(sql.toString());
		}
		return ResponseResult.ok(count);
	}

}
